package com.sinoglobal.sinostore.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.sinoglobal.sinostore.R;
import com.sinoglobal.sinostore.bean.ShopWareList;
import com.sinoglobal.sinostore.system.Constants;
import com.sinoglobal.sinostore.utils.BitmapHelp;
import com.sinoglobal.sinostore.utils.TextUtil;
/**
 * 
* @title: GoodsRecommendedAdapter.java 
* @package com.chiliring.sinostore.adapter 
* @description: TODO(商品推荐) 
* @author LK
* @date 2015-1-28 下午4:40:51
 */
public class GoodsRecommendedAdapter extends BaseAdapter {
	
	private Context context;
	private ArrayList<ShopWareList> data;
	private BitmapUtils bitmapUtils;
	
	public GoodsRecommendedAdapter(Context context){
		this.context = context;
		data = new ArrayList<ShopWareList>();
		bitmapUtils = BitmapHelp.getBitmapUtils(context);
	}

	public void addData(ArrayList<ShopWareList> data){
		if(null != data){
			this.data.addAll(data);
		}
		notifyDataSetChanged();
	}
	
	public void clearData(){
		if(null != data){
			data.clear();
		}
		notifyDataSetChanged();
	}
	
	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public ShopWareList getItem(int position) {
		return data.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if(null == convertView){
			convertView = LayoutInflater.from(context).inflate(R.layout.shop_item_recommended_layout, null);
		}
		ImageView imgGoods = ViewHolder.getViewById(convertView, R.id.img_goods_pic);
		TextView tvHaveToBuy = ViewHolder.getViewById(convertView, R.id.tv_have_to_buy);
		TextView tvGoodsName = ViewHolder.getViewById(convertView, R.id.tv_goods_name);
		TextView tvGoodsPrice = ViewHolder.getViewById(convertView, R.id.tv_goods_price);
		ShopWareList entity = data.get(position);
		bitmapUtils.display(imgGoods, Constants.PIC_HOST+entity.getImg_url());
		tvGoodsName.setText(entity.getName());
		tvHaveToBuy.setText(String.format("已购买%s", entity.getBum_convert()));
		if(TextUtil.isZeroOrNull(entity.getPrice()) && !TextUtil.isZeroOrNull(entity.getCash())){//积分单价为零，现金不为零，为现金支付
			tvGoodsPrice.setText(String.format("%s元", entity.getCash()));
		}else if(TextUtil.isZeroOrNull(entity.getCash()) && !TextUtil.isZeroOrNull(entity.getPrice())){//现金单价为零，为积分支付
			tvGoodsPrice.setText(String.format("%s%s", entity.getPrice(),context.getString(R.string.score_unit)));
		}else{//两个都不为零则为现金加积分支付
			tvGoodsPrice.setText(String.format("%s元+%s%s", entity.getCash(),entity.getPrice(),context.getString(R.string.score_unit)));
		}
		return convertView;
	}

}
